package self;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import org.junit.Test;


/**
 * @author
 * @since 2018/12/13
 */
public class CodeGenerator {

    @Test
    public void run() {
        // 1、创建代码生成器
        AutoGenerator mpg = new AutoGenerator();

        // 2、全局配置
        GlobalConfig gc = new GlobalConfig();
        //下面这个定位的是 你工程的当前的更目录 但是 一般情况都会出错
        // 那么你就可以 不用
//        String projectPath = System.getProperty("user.dir");
        //输出目录
//        gc.setOutputDir(projectPath + "/src/main/java");
        //第一个要改的地方(改成你项目的根目录)
        gc.setOutputDir("F:\\java练习学习\\WX_Self_Iot_parent\\iot-service-parent\\iot-web-server\\src\\main\\java");
        //作者(这里也可以改)
        gc.setAuthor("liYU");
        gc.setOpen(false); //生成后是否打开资源管理器
        gc.setFileOverride(false); //重新生成时文件是否覆盖
        gc.setServiceName("%sService");	//去掉Service接口的首字母I  默认有I
        gc.setIdType(IdType.ID_WORKER_STR); //主键策略
        gc.setDateType(DateType.ONLY_DATE);//定义生成的实体类中日期类型
        gc.setSwagger2(true);//开启Swagger2模式
        mpg.setGlobalConfig(gc);

        // 3、数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        //  这里的数据库名字需要进行修改
        dsc.setUrl("jdbc:mysql://localhost:3306/wx_self_iot?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8");
        dsc.setDriverName("com.mysql.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("root");
        dsc.setDbType(DbType.MYSQL);
        mpg.setDataSource(dsc);

        // 4、包配置
        PackageConfig pc = new PackageConfig();
        pc.setModuleName("iotService"); //模块名
        pc.setParent("self");   //这里写根包
        pc.setController("controller");
        pc.setEntity("pojo");
        pc.setService("service");
        pc.setMapper("mapper");
        mpg.setPackageInfo(pc);

        // 5、策略配置
        StrategyConfig strategy = new StrategyConfig();
        //这里更改表
        //生成多张表的增删改查（表的名字需要修改）
        strategy.setInclude("qf_area","qf_device","qf_device_model","qf_device_topic","qf_device_type","qf_group","qf_industry","qf_menu","qf_model_data","qf_model_template","qf_product","qf_role","qf_role_menu","qf_sys_dict_data","qf_sys_dict_type","qf_tenant","qf_user","qf_user_role");
        //生成一张表的增删改查
       /* strategy.setInclude("t_ad","t_class","t_class_classpackage","t_classdirectory","t_classpackage",
                "t_classtype",
                "t_customer",
                "t_news",
                "t_newstype",
                "t_order",
                "t_permissionresource",
                "t_poster",
                "t_question",
                "t_role",
                "t_role_permission",
                "t_stu_result",
                "t_stu_result_type",
                "t_teacher",
                "t_teacher_class",
                "t_user",
                "t_user_class",
                "t_admin_role",
                "t_userbehavior",
                "t_video",
                "t_admin");*/
//        strategy.setInclude("t_admin_user","t_role","t_menu","t_role_menu","t_admin_role");

        strategy.setNaming(NamingStrategy.underline_to_camel);//数据库表映射到实体的命名策略
//        strategy.setTablePrefix(pc.getModuleName() + "t_"); //生成实体时去掉表前缀
        strategy.setTablePrefix("qf_"); //生成实体时去掉表前缀

        strategy.setColumnNaming(NamingStrategy.underline_to_camel);//数据库表字段映射到实体的命名策略
        strategy.setEntityLombokModel(true); // lombok 模型 @Accessors(chain = true) setter链式操作

        strategy.setRestControllerStyle(true); //restful api风格控制器
        strategy.setControllerMappingHyphenStyle(true); //url中驼峰转连字符

        mpg.setStrategy(strategy);

        // 6、执行
        mpg.execute();
    }
}
